import java.util.Collections;
import java.util.Scanner;
import java.util.ArrayList;
public class student_main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<student> arr = new ArrayList<>();

        while (true) {
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.查看所有学生");
            System.out.println("4.查找学生");
            //System.out.println("5.学生绩点降序");
            System.out.println("6.退出");
            System.out.println("输入你的选择：");
            int n = sc.nextInt();
            switch (n) {

                case 1 -> addStudent(arr);
                case 2 -> deleteStudent(arr);
                case 3 -> showStudent(arr);
                case 4 -> findStudent(arr);
               // case 5 -> descending(arr);
                case 6 -> System.exit(0);
            }
        }
    }

    public static void addStudent(ArrayList<student> arr) {
        Scanner sc = new Scanner(System.in);
        String sID;
        while (true) {
            System.out.println("学号");
            sID = sc.nextLine();
            if (isUsed(arr, sID)) {
                System.out.println("学号输入重复，请重新输入");
            } else break;
        }

        System.out.println("姓名");
        String name = sc.nextLine();
        System.out.println("年龄");
        String age = sc.nextLine();
        System.out.println("性别");
        String gender = sc.nextLine();
        System.out.println("绩点");
        String GPA = sc.nextLine();


        student s = new student();
        s.setSid(sID);
        s.setName(name);
        s.setAge(age);
        s.setGender(gender);
        s.setGPA(GPA);

        arr.add(s);
        System.out.println("添加学生成功!");
    }

    public static void deleteStudent(ArrayList<student> arr) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除学生的学号：");
        String sID = sc.nextLine();
        boolean index = true;
        for (int i = 0; i < arr.size(); i++) {
            student s = arr.get(i);
            if (s.getSid().equals(sID)) {
                arr.remove(i);
                index = false;
                System.out.println("删除成功");
                break;
            }
        }
        if (index) {
            System.out.println("学号输入错误");
        }
    }

    public static void findStudent(ArrayList<student> arr) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要查找学生的学号：");
        if (arr.size() == 0) {
            System.out.println("无数据，请先输入数据");
            return;
        }
        String sID = sc.nextLine();
        boolean index = true;
        for (int i = 0; i < arr.size(); i++) {
            student s = arr.get(i);
            if (s.getSid().equals(sID)) {
                index = false;
                System.out.println("该学生的信息如下："+"\n"+"姓名:"+s.getName()+"\t"+"学号:"+s.getSid()+"\t"+"性别:"+s.getGender()+"\t"+"年龄:"+s.getAge()+"\t"+"绩点:"+s.getGPA());
                break;
            }
        }
        if (index) {
            System.out.println("学号输入错误");
        }
    }

    public static void showStudent(ArrayList<student> arr) {

        if (arr.size() == 0) {
            System.out.println("无数据，请先输入数据");
            return;
        }

        System.out.println("学号" + "\t" + "姓名" + "\t" + "年龄" + "\t" + "性别");

        for (student s : arr) {
            System.out.println(s.getSid() + "\t" + s.getName() + "\t" + s.getAge() + "\t"+ s.getGender()+ "\t"+ s.getGPA());
        }
    }

   /* public static void descending(ArrayList<student> arr) {
        int i = 0;
        student s = arr.get(i);
        Collections.reverse(s.getGPA())

    }

    */
    public static boolean isUsed(ArrayList<student> arr, String sID) {
        boolean n = false;
        for (student s : arr) {
            if (s.getSid().equals(sID)) {
                n = true;

                break;
            }
        }
        return n;
    }
}


